package GUIs;
import java.awt.BorderLayout;
import java.awt.Font;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import DeportManage.Parcel;
import MVC.*;


public class ParcelPanel extends JPanel implements Observer{

	private DataObserver data_observer;
	private JTextArea parcel_table;
	/**
	 * Constructor to create an object  of Parcel with data_observer and initial a set of valriable
	 * @param data_observer the object of Observer pattern 
	 */
	public ParcelPanel(DataObserver data_observer){		
		this.data_observer = data_observer;
		data_observer.registerObserver(this);
		
		this.setLayout(new BorderLayout(5,5));		

		parcel_table = new JTextArea();
		parcel_table.setSize(100, 50);
		parcel_table.setEditable(false);
		parcel_table.setFont(new Font("Arial",Font.LAYOUT_LEFT_TO_RIGHT, 15));
		JScrollPane parcel_scollpane = new JScrollPane(parcel_table);
		this.add(new JLabel("Parcel List :"),BorderLayout.NORTH);
		this.add(parcel_scollpane,BorderLayout.CENTER);
		
		update();
	}
	/**
	 * Method to update and show the list of parcels
	 */
	@Override
	public void update() {
		String parcel_text ="Parcel ID      Days     Weight       Dimension    \n";
		parcel_text += "-----------------------------------------------------------\n";
		int index = 0;		
		HashMap<String, Parcel> parcel_map = data_observer.getParcelList().getParcel();
		Iterator<Entry<String, Parcel>> iter = parcel_map.entrySet().iterator(); 
		while (iter.hasNext()) { 
			Map.Entry<String, Parcel> entry = (Map.Entry<String, Parcel>)iter.next();
			Parcel parcel = entry.getValue();
			parcel_text += String.format("%-16s", parcel.getID());
			parcel_text += String.format("%-14s", parcel.getDays());
			parcel_text += String.format("%-14s", parcel.getWeight());
			parcel_text += String.format("%-14s", parcel.getDimension());
			parcel_text += "\n-----------------------------------------------------------";
			parcel_text += "\n";
			index++;
		}
		
		this.parcel_table.setText(parcel_text);
	}
}
